//
//  TSZStatusCell.swift
//  微博天下
//
//  Created by Tsz on 15/10/13.
//  Copyright © 2015年 Tsz. All rights reserved.
//

import UIKit

//定义一个 切换cell标识符的枚举
enum StatausCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ReprintCell = "ReprintCell"
    
    //静态函数
    static func cellId(data: TSZHomeMircoblogModel) ->String{
        return data.retweeted_status == nil ?
        StatausCellIdentifier.NormalCell.rawValue : StatausCellIdentifier.ReprintCell.rawValue
        
    }
}

protocol StatusCellDelegate:NSObjectProtocol {
    
    func statusCellDidClickURL(urlString: String)
}

class TSZStatusCell: UITableViewCell {
    
    weak var statusDelegate: StatusCellDelegate?
    //加载微博数据
    var data: TSZHomeMircoblogModel?{
        didSet{
            topView.data = data
            let stringText = data?.text ?? ""
            contentLabel.attributedText = TSZEmoticonsPackage.emoticonText(stringText, font: contentLabel.font)
            
            //给我们的图片传入模型
            pictureView.mricoblogData = data
            
            //设置配图试图的尺寸
            pictureWigthCons?.constant = pictureView.bounds.size.width
            pictureHightCons?.constant = pictureView.bounds.size.height
             pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusPictureMargin
        }
    }
    //图片宽度约束
    var pictureWigthCons: NSLayoutConstraint?
    //图片宽度高度
    var pictureHightCons: NSLayoutConstraint?
    /// 图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    //计算行高
    func rowHight(data: TSZHomeMircoblogModel) ->CGFloat{
        //设置属性
        self.data = data
        
        //强行更新布局
        layoutIfNeeded()
        //返回底部试图的最大高度
        return CGRectGetMaxY(buttomView.frame)
        
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //界面
     func setupUI(){
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        
        contentView.addSubview(buttomView)
        
        //自动布局
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height:53))
        //布局内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil , offset: CGPoint(x: statusPictureMargin, y: statusPictureMargin))
        //布局宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusPictureMargin))
        
        //底部布局
        buttomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44),offset: CGPoint(x: -statusPictureMargin , y: statusPictureMargin))
        //底部约束
        
//        contentView.addConstraint(NSLayoutConstraint(item: buttomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    //MARK - 懒加载 控件
    /**
    * 顶部试图
    */
    private lazy var topView : TSZStatusTopView = TSZStatusTopView()
    //内容标签
    
     lazy var contentLabel: FFLabel =  {
        let label = FFLabel(color: UIColor.darkGrayColor(), fontSize: 15)
        
        label.labelDelegate = self
        
        label.numberOfLines = 0
        //设置文本折行的宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusPictureMargin
        
        return label
    }()
    
    //加载图片
     lazy var pictureView:TSZPictureView = TSZPictureView()
    
    //底部试图
    lazy var buttomView:TSZStatusButtomView = TSZStatusButtomView()
    
}
extension TSZStatusCell: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
    if text.hasPrefix("http://"){
            statusDelegate?.statusCellDidClickURL(text)
        }
    }
}
